package logic

import (
	"context"
	"group/common"
	"group/models"

	"group/internal/svc"
	"group/pb/group"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateApplyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateApplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateApplyLogic {
	return &CreateApplyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateApply 创建群聊申请
func (l *CreateApplyLogic) CreateApply(in *group.ApplyInfo) (*group.Empty, error) {
	result, err := l.svcCtx.GroupApplyModel.Insert(l.ctx, &models.GroupApply{
		GroupId: in.GroupId,
		UserId:  in.UserId,
		Message: in.Message,
		Status:  common.GroupApplyApply,
	})
	if err != nil {
		return nil, err
	}

	lastID, err := result.LastInsertId()
	if err != nil {
		return nil, err
	}

	in.Id = lastID
	l.Logger.Infof("add group apply: %v", in)
	return &group.Empty{}, nil
}
